/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pacificlms.MainPanel;

import com.mysql.jdbc.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.DefaultListModel;
import pacificlms.MasterConnection;
import pacificlms.Verifiable.Database.Member;
import pacificlms.Verifiable.VerifiableObject;

/**
 *
 * @author Yasir Tanveer
 */
public class SearchForMember extends CentralUIPanel {

    /**
     * Creates new form SearchForMember
     */
    public SearchForMember(boolean ShowSelect) {
        super(null);
        initComponents();
        
        btnSelect.setVisible(ShowSelect);
        btnView.setVisible(!ShowSelect);
    }
    public String TabName()
    {
        return "Member Search";
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel11 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtStudentNumber = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtPhone = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtLibraryCard = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        btnClose = new javax.swing.JButton();
        btnSearch = new javax.swing.JButton();
        btnSelect = new javax.swing.JButton();
        JScrollPane = new javax.swing.JScrollPane();
        lstSearch = new javax.swing.JList();
        btnView = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(615, 535));
        setLayout(null);

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 22)); // NOI18N
        jLabel11.setText("Search for Member");
        add(jLabel11);
        jLabel11.setBounds(10, 0, 351, 27);

        jLabel1.setText("Name");
        add(jLabel1);
        jLabel1.setBounds(10, 114, 65, 14);

        txtName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNameActionPerformed(evt);
            }
        });
        add(txtName);
        txtName.setBounds(134, 111, 301, 20);

        jLabel2.setText("Student/Staff #");
        add(jLabel2);
        jLabel2.setBounds(10, 62, 120, 14);

        txtStudentNumber.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtStudentNumberActionPerformed(evt);
            }
        });
        add(txtStudentNumber);
        txtStudentNumber.setBounds(134, 59, 96, 20);

        jLabel3.setText("Phone");
        add(jLabel3);
        jLabel3.setBounds(10, 140, 78, 14);

        txtPhone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPhoneActionPerformed(evt);
            }
        });
        add(txtPhone);
        txtPhone.setBounds(134, 137, 119, 20);

        jLabel4.setText("Library Card #");
        add(jLabel4);
        jLabel4.setBounds(10, 36, 110, 14);

        txtLibraryCard.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtLibraryCardActionPerformed(evt);
            }
        });
        add(txtLibraryCard);
        txtLibraryCard.setBounds(134, 33, 124, 20);

        jLabel5.setText("Email");
        add(jLabel5);
        jLabel5.setBounds(10, 88, 78, 14);

        txtEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEmailActionPerformed(evt);
            }
        });
        add(txtEmail);
        txtEmail.setBounds(134, 85, 356, 20);

        jLabel6.setText("Matching Members");
        add(jLabel6);
        jLabel6.setBounds(10, 174, 180, 14);

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClosejButton8ActionPerformed(evt);
            }
        });
        add(btnClose);
        btnClose.setBounds(496, 501, 110, 23);

        btnSearch.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchjButton8ActionPerformed(evt);
            }
        });
        add(btnSearch);
        btnSearch.setBounds(496, 163, 110, 25);

        btnSelect.setText("Select");
        btnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectjButton8ActionPerformed(evt);
            }
        });
        add(btnSelect);
        btnSelect.setBounds(10, 501, 110, 23);

        JScrollPane.setViewportView(lstSearch);

        add(JScrollPane);
        JScrollPane.setBounds(10, 194, 596, 301);

        btnView.setText("View");
        btnView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewActionPerformed(evt);
            }
        });
        add(btnView);
        btnView.setBounds(10, 501, 110, 23);
    }// </editor-fold>//GEN-END:initComponents

    private void txtNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNameActionPerformed

    private void txtStudentNumberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtStudentNumberActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtStudentNumberActionPerformed

    private void txtPhoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPhoneActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPhoneActionPerformed

    private void txtLibraryCardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtLibraryCardActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtLibraryCardActionPerformed

    private void txtEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtEmailActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtEmailActionPerformed

    private void btnClosejButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClosejButton8ActionPerformed
        Close();
    }//GEN-LAST:event_btnClosejButton8ActionPerformed

    void SearchMember()
    {
        Member Memb = new Member();
        Memb.LibraryCardNumber(txtLibraryCard.getText());
        Memb.IdentityNumber(txtStudentNumber.getText());
        Memb.Name(txtName.getText());
        Memb.PhoneNumber(txtPhone.getText());
        Memb.Email(txtEmail.getText());
        
        String SQL = Memb.FindSimilarSQL();
 
        try {
            java.sql.Connection con = MasterConnection.getInstance();
            Statement stmt = con.createStatement();
            
            ResultSet rs;
            rs = stmt.executeQuery(SQL);
            ResultSetMetaData RMS = rs.getMetaData();
            
            DefaultListModel model = new DefaultListModel();
            
            boolean Found = false;
            while (rs.next()) {
                Found = true;
                Member M = (Member) VerifiableObject.CreateFromResultSet(Member.class, rs, RMS);
                model.add(model.getSize(), M);
            }
            if (!Found)
            {
                model.addElement("Member not found!");
            }
            
            lstSearch.setModel(model);

        }
        catch(Exception Ex)
        {
            System.out.println(Ex.toString());
        }
    }
    
    private void btnSearchjButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchjButton8ActionPerformed
        SearchMember();
    }//GEN-LAST:event_btnSearchjButton8ActionPerformed

    private void btnSelectjButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectjButton8ActionPerformed
        if (lstSearch.getSelectedValue() != null && lstSearch.getSelectedValue().getClass() != String.class) {
            this.CurrentObject = (VerifiableObject) lstSearch.getSelectedValue();
        }
        Close();
    }//GEN-LAST:event_btnSelectjButton8ActionPerformed

    private void btnViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewActionPerformed
        if (lstSearch.getSelectedValue() != null && lstSearch.getSelectedValue().getClass() != String.class)
            this.CurrentObject = (VerifiableObject) lstSearch.getSelectedValue();
        MainPanel.GetMainPanel().AddCentralUIPanel(new InformationMember(this.CurrentObject));
    }//GEN-LAST:event_btnViewActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane JScrollPane;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnSelect;
    private javax.swing.JButton btnView;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JList lstSearch;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtLibraryCard;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtPhone;
    private javax.swing.JTextField txtStudentNumber;
    // End of variables declaration//GEN-END:variables
}
